using FlowCanvas;
using FlowCanvas.Nodes;
using LogSystem;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.AI.Node
{
    [Category("Ggame/Liberty")]
    public class AddStaticNpcSimple : CallableActionNodeBase
    {
        [HideInInspector]
        public FlowOutput onSuccess;
        [HideInInspector]
        public FlowOutput onFailed;
        
        private void Invoke(int uniqueId, string pointName, int npcModelId, int graphId,string animState)
        {
            pointName = pointName.Trim();
            BluePrintPoint point = BlueprintUtils.GetBlueprintPoint(pointName);
            if (point != null)
            {
                AIBirthData birthData = new AIBirthData();
                birthData.uniqueId = uniqueId;
                birthData.position = point.position;
                birthData.forward = point.forward;
                birthData.isStatic = true;
                birthData.modelResId = npcModelId;
                birthData.maxSpeed = 0f;
                if (npcModelId == -1)
                {
                    birthData.randomModel = true;
                }
                birthData.animState = animState;

                if (BlueprintUtils.LibertyManager.AIManager.AddNpc(birthData, graphId))
                {
                    onSuccess?.Call(new Flow());
                }
                else
                {
                    onFailed?.Call(new Flow());
                }
                
            }
            else
            {
                Debug.LogError(LogModule.Blueprint, $"找不到蓝图点：{pointName}");
                onFailed?.Call(new Flow());
            }
        }

        protected override void OnRegisterPorts(FlowNode node)
        {
            var p1 = node.AddValueInput<int>("uniqueId");
            var p2 = node.AddValueInput<string>("pointName");
            var p3 = node.AddValueInput<int>("npcModelId");     // -1表示随机
            var p4 = node.AddValueInput<int>("graphId");
            var p5 = node.AddValueInput<string>("animState");
            
            var o = node.AddFlowOutput("Out");
            node.AddFlowInput("In", (f) =>
            {
                Invoke(p1.value, p2.value, p3.value, p4.value, p5.value);
                o.Call(f);
            });

            onFailed = node.AddFlowOutput("onFailed");
            onSuccess = node.AddFlowOutput("onSuccess");
            
            node.AddValueOutput<int>("uniqueId", () =>
            {
                return p1.value;
            });
        }
    }
}